﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class ScheduleClassPage : System.Web.UI.Page
    {
        CourseSchedule classTimeSchedule = new CourseSchedule();
        ScheduleManager scheduleManager = new ScheduleManager();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }

            if (IsPostBack != true)
            {
                PopulatedDepartmentDropDownList();
                PopulatedSemesterDropDownList();
                PopulatedCourseDropDownList();
                PopulatedBuildingDropDownList();
                PopulatedRoomDropDownList();
                PopulateAmPmDropDownList();
                PopulatedDayDropDownList();
            }
            
        }

        private void PopulatedDayDropDownList()
        {
            DayManager dayManager = new DayManager();
            DataTable table = dayManager.GetAllDays();
            dayDropDownList.DataSource = table;
            dayDropDownList.DataTextField = "Key";
            dayDropDownList.DataValueField = "Value";
            dayDropDownList.DataBind();
        }

        


        private void PopulateAmPmDropDownList()
        {
            amPmDropDownList.Items.Add(new ListItem("AM", "AM"));
            amPmDropDownList.Items.Add(new ListItem("PM", "PM"));
        }
        private void PopulatedCourseDropDownList()
        {
            CourseManager courseManager = new CourseManager();
            DataTable courseTable = courseManager.GetAllCourses();
            courseDropDownList.DataSource = courseTable;
            courseDropDownList.DataTextField = "CourseName";
            courseDropDownList.DataValueField = "Id";
            courseDropDownList.DataBind();

        }
        private void PopulatedDepartmentDropDownList()
        {
            DepartmentManager departmentManager = new DepartmentManager();
            DataTable departments = departmentManager.GetAllDepartments();
            departmentDropDownList.DataTextField = "Name";
            departmentDropDownList.DataValueField = "Id";
            departmentDropDownList.DataSource = departments;
            departmentDropDownList.DataBind();
        }
        private void PopulatedSemesterDropDownList()
        {
            SemesterManager semesterManager = new SemesterManager();
            DataTable semesters = semesterManager.GetAllSemester();
            semesterDropDownList.DataTextField = "SemesterName";
            semesterDropDownList.DataValueField = "Id";
            semesterDropDownList.DataSource = semesters;
            semesterDropDownList.DataBind();
        }

        private void PopulatedBuildingDropDownList()
        {
            BuildingManager buildingManager = new BuildingManager();
            DataTable buildings = buildingManager.GetAllBulidings();
            buildingDropDownList.DataTextField = "BuildingName";
            buildingDropDownList.DataValueField = "Id";
            buildingDropDownList.DataSource = buildings;
            buildingDropDownList.DataBind();
        }

        private void PopulatedRoomDropDownList()
        {
            RoomManager roomManager = new RoomManager();
            DataTable rooms = roomManager.GetAllRooms();
            roomDropDownList.DataTextField = "Room";
            roomDropDownList.DataValueField = "Id";
            roomDropDownList.DataSource = rooms;
            roomDropDownList.DataBind();
        }
        private void PopulateCourseDropDownListForSelectedDepartment(int selectedDepartment)
        {
            CourseManager courseManager = new CourseManager();
            DataTable cousreTable = courseManager.GetAllCoursesOf(selectedDepartment);
            courseDropDownList.DataSource = cousreTable;
            courseDropDownList.DataTextField = "Code";
            courseDropDownList.DataValueField = "Id";
            courseDropDownList.DataBind();

        }

        protected void semesterDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int selectedDepartment = Convert.ToInt32(departmentDropDownList.SelectedValue);
                PopulateCourseDropDownListForSelectedDepartment(selectedDepartment);
            }
            catch (Exception exception)
            {
                classScheduleMessageLabel.Text = exception.Message;
            }
        }

        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void scheduleClassSaveButton_Click(object sender, EventArgs e)
        {

             
            if (timeFromTextBox.Text!=string.Empty && timeToTextBox.Text!=string.Empty)
            {
                try
                {
                    classTimeSchedule.Department.DeptId = Convert.ToInt32(departmentDropDownList.SelectedValue);
                    classTimeSchedule.Semester.Id = Convert.ToInt32(semesterDropDownList.SelectedValue);
                    classTimeSchedule.Course.CourseId = Convert.ToInt32(courseDropDownList.SelectedValue);
                    classTimeSchedule.Building.Id = Convert.ToInt32(buildingDropDownList.SelectedValue);
                    classTimeSchedule.Room.Id = Convert.ToInt32(roomDropDownList.SelectedValue);
                    classTimeSchedule.Day = dayDropDownList.SelectedValue;
                    classTimeSchedule.Times.StartingTime =timeFromTextBox.Text + " " + amPmDropDownList.SelectedValue;
                    classTimeSchedule.Times.EndingTime =  timeToTextBox.Text   + " " + amPmDropDownList.SelectedValue;
                    string message = (scheduleManager.SaveClassSchedule(classTimeSchedule));
                    classScheduleMessageLabel.ForeColor = Color.Green;
                    classScheduleMessageLabel.Text = message;


                }

                 catch(FormatException exception)
                 {
                     classScheduleMessageLabel.ForeColor = Color.Red;
                     classScheduleMessageLabel.Text = exception.Message;

                 }
                catch (Exception exception)
                {

                    classScheduleMessageLabel.ForeColor = Color.Red;
                    classScheduleMessageLabel.Text = exception.Message;
                }  
                
            }
            else
            {
                classScheduleMessageLabel.ForeColor = Color.Red;
                classScheduleMessageLabel.Text = "Please Insert Time field Properly";
            }
            ClearTextBoxes();

        }

        private void ClearTextBoxes()
        {
            timeFromTextBox.Text = string.Empty;
            timeToTextBox.Text = string.Empty;
        }

        protected void timeFromAmPmDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}